package com.onesports.intelligent.k12.polarlight.service.course;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.framework.kit.common.util.BeanUtils;
import com.onesports.framework.kit.common.util.ParamUtils;
import com.onesports.intelligent.k12.polarlight.common.domain.entity.BaseEntity;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.ClassStudent;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.CourseScheduleDetail;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.SpellingClassDetails;
import com.onesports.intelligent.k12.polarlight.domain.entity.course.SpellingClassPeriod;
import com.onesports.intelligent.k12.polarlight.domain.vo.course.CourseScheduleDetailVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.student.StudentAttendanceVO;
import com.onesports.intelligent.k12.polarlight.mapper.course.CourseScheduleDetailMapper;
import com.onesports.intelligent.k12.polarlight.mapper.course.SpellingClassDetailsMapper;
import com.onesports.intelligent.k12.polarlight.service.student.ClassStudentService;
import com.onesports.intelligent.k12.polarlight.service.student.StudentAttendanceService;
import com.onesports.intelligent.k12.polarlight.service.student.UserStudentRelService;
import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Wsy
 **/
@Service
@RequiredArgsConstructor
public class CourseScheduleDetailService extends ServiceImpl<CourseScheduleDetailMapper, CourseScheduleDetail> {

    private final UserStudentRelService userStudentRelService;
    private final StudentAttendanceService studentAttendanceService;
    private final ClassStudentService classStudentService;
    private final SpellingClassPeriodService spellingClassPeriodService;
    private final CourseScheduleDetailMapper courseScheduleDetailMapper;


    /**
     * 小程序查看课表
     *
     * @param classId 班级id
     * @return List<CourseScheduleDetailVO>
     */
    public List<CourseScheduleDetailVO> view(String classId) {
       return this.spellingClassPeriodService.listByClassId(classId);
    }


    /**
     * 获取课表教练学员信息
     *
     * @param scheduleDetailId 时段明细id
     * @return List<ScheduleStuInfoVO>
     */
    public List<StudentAttendanceVO> coachScheduleStuInfo(String scheduleDetailId, String classId) {
        List<ClassStudent> classStudents = this.classStudentService.currentStu(classId);
        if (ParamUtils.isEmpty(classStudents)){
            return new ArrayList<>();
        }
        List<String> classStudentIds = classStudents.stream().map(BaseEntity::getId).collect(Collectors.toList());
        //获取学员考勤信息
        return this.studentAttendanceService.stuAttendanceInfo(Collections.singletonList(scheduleDetailId), classStudentIds);
    }


    /**
     * 获取课表会员学员信息
     *
     * @param scheduleDetailId 时段明细id
     * @return List<ScheduleStuInfoVO>
     */
    public List<StudentAttendanceVO> vipScheduleStuInfo(String scheduleDetailId, String accountId, String classId) {
        //获取家长的学员
        List<String> studentIds = this.userStudentRelService.getStudentIdListByUserId(accountId);
        //根据班级和家长学员获取当前课表对应班级下的学员
        List<ClassStudent> classStudents = this.classStudentService.currentStu(classId, studentIds);
        if (ParamUtils.isEmpty(classStudents)){
            return new ArrayList<>();
        }
        List<String> classStudentIds = classStudents.stream().map(BaseEntity::getId).collect(Collectors.toList());
        //获取学员考勤信息
        return this.studentAttendanceService.stuAttendanceInfo(Collections.singletonList(scheduleDetailId), classStudentIds);
    }


    /**
     * 班级管理-查看详情-排课表
     *
     * @return List<CourseScheduleDetailVO>
     */
    public List<CourseScheduleDetailVO> getScheduleDetail(String classId) {
        return this.spellingClassPeriodService.listByClassId(classId);
    }

    public List<CourseScheduleDetail> listByScheduleIds(List<String> scheduleIds) {
        return this.list(new LambdaQueryWrapper<CourseScheduleDetail>()
                .in(CourseScheduleDetail::getScheduleId, scheduleIds));
    }

    public List<CourseScheduleDetail> geScheduleDetail(CourseScheduleDetail scheduleDetail) {
        return this.courseScheduleDetailMapper.geScheduleDetail(scheduleDetail);
    }

    public List<CourseScheduleDetail> ltScheduleDetail(CourseScheduleDetail scheduleDetail) {
        return this.courseScheduleDetailMapper.ltScheduleDetail(scheduleDetail);
    }


    /**
     * 重新统计应到人数
     */
    public void recountDueNum(){
        List<SpellingClassPeriod> list = this.spellingClassPeriodService.list();
        Map<String, List<SpellingClassPeriod>> map = list.stream().collect(Collectors.groupingBy(SpellingClassPeriod::getCourseScheduleDetailId));
        Set<String> scheduleDetailIds = list.stream().map(SpellingClassPeriod::getCourseScheduleDetailId).collect(Collectors.toSet());
        List<String> detailIds = new ArrayList<>(scheduleDetailIds);
        Map<String, Integer> scheduleDetailMap = new HashMap<>();
        map.forEach((k,v)->{
            int totalNum = v.stream().mapToInt(SpellingClassPeriod::getDueNum).reduce(0, Integer::sum);
            scheduleDetailMap.put(k , totalNum);
        });
        List<CourseScheduleDetail> courseScheduleDetails = this.listByIds(detailIds);
        courseScheduleDetails.forEach(i->{
            Integer dueNum = scheduleDetailMap.get(i.getId());
            if (ParamUtils.isNotEmpty(dueNum)){
                i.setDueNum(dueNum);
            }
        });

        this.updateBatchById(courseScheduleDetails);

    }
}
